Skip to content

Conversation

@tmandry
Copy link
Member

@tmandry tmandry commented Jan 22, 2026

A small example to illustrate the difference between array and trait unsized coercions: (playground)

fn check_dyn(t: &Type) {
    check_is_unsize::<Type, dyn Trait>(); // Success
    t.method(); //~ERROR
}

fn check_array(t: &[i32; 3]) {
    check_is_unsize::<[i32; 3], [i32]>(); // Success
    t.len(); // Success
    // ^ Note that len is only defined on [T].
}

fn check_is_unsize<From: ?Sized, To: ?Sized>()
where
    From: std::marker::Unsize<To>,
{}

struct Type;

trait Trait {}

impl dyn Trait {
    fn method(&self) {}
}

impl Trait for Type {}

This came up during a discussion about autoref in zulip: #t-lang/custom-refs > Autoref. Thanks to @BennoLossin for the original example.

The rustc dev guide describes this algorithm similarly to the way the reference does today; it mentions unsized coercions in general, but only uses arrays as an example.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jan 22, 2026
@tmandry
Copy link
Member Author

tmandry commented Jan 24, 2026

@BennoLossin and @Nadrieril pointed out that we could remove this step entirely with a simple Deref impl for arrays:

impl<T, const N: usize> Deref for [T; N] {
    type Target = [T];
    fn deref(&self) -> &[T] { &*self }
}

Similarly for DerefMut. I wonder why those impls don't exist.

@rustbot

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@traviscross
Copy link
Contributor

This is true. Thanks for the PR.

@traviscross traviscross added this pull request to the merge queue Jan 28, 2026
@traviscross traviscross removed the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jan 28, 2026
Merged via the queue into master with commit 8938c4b Jan 28, 2026
5 checks passed
@tmandry tmandry deleted the tmandry-patch-2 branch January 28, 2026 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants